#include <54func.h>
typedef struct shareRes_s {
    int flag;
    pthread_mutex_t mutex;
} shareRes_t;
void A(){
    printf("Before A\n");
    sleep(3);
    printf("After A\n");
}
void B(){
    printf("Before B\n");
    sleep(3);
    printf("After B\n");
}
void *threadFunc(void *arg){
    shareRes_t * pshareRes = (shareRes_t *)arg;
    while(1){
        pthread_mutex_lock(&pshareRes->mutex);
        if(pshareRes->flag == 1){
            pthread_mutex_unlock(&pshareRes->mutex);
            break;
        }
        pthread_mutex_unlock(&pshareRes->mutex);
    }
    B();
    pthread_exit(NULL);
}
int main(int argc, char *argv[])
{
    shareRes_t shareRes;
    shareRes.flag = 0;
    pthread_mutex_init(&shareRes.mutex,NULL); 
    pthread_t tid;
    pthread_create(&tid,NULL,threadFunc,&shareRes);

    sleep(3);
    A();
    pthread_mutex_lock(&shareRes.mutex);
    shareRes.flag = 1;
    pthread_mutex_unlock(&shareRes.mutex);

    pthread_join(tid,NULL);
    return 0;
}

